﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.IO;
using System.Text.RegularExpressions;

namespace GeneratorLibrary.Client.Base {
    public class StringCodeBuilder {
        int tabs = 0;
        StringBuilder sb = new StringBuilder();
        public void Append(string value) {
            string[] values = value.Split(new char[] { '\n' });
            for (int i = 0; i < values.Length - 1; i++) {
                AddTabsAndAppend(values[i]);
                sb.Append('\n');
            }
            AddTabsAndAppend(values[values.Length - 1]);
        }
        private void Append(string value, bool fromNewLine) {
            string[] values = value.Split(new char[] { '\n' });
            for (int i = 0; i < values.Length - 1; i++) {
                if (fromNewLine)
                    AddTabsAndAppend(values[i]);
                else
                    sb.Append(values[i]);
                sb.Append('\n');
                fromNewLine = true;
            }
            if (fromNewLine)
                AddTabsAndAppend(values[values.Length - 1]);
            else
                sb.Append(values[values.Length - 1]);
            fromNewLine = true;
        }

        private void AddTabsAndAppend(string s) {
            if (s.Length > 0) {
                for (int i = 0; i < tabs; i++)
                    sb.Append('\t');
                sb.Append(s);
            }
        }
        public void AppendInLine(string value) {
            sb.Append(value);
        }
        public void AppendBeginAfter(string value) {
            Append(value);
            AddTab();
        }
        public void AppendEndBefore(string value) {
            RemoveTab();
            Append(value);
        }
        public void AddTab() {
            tabs++;
        }
        public void RemoveTab() {
            if (tabs > 0)
                tabs--;
        }

        public void AppendBraces(string value) {
            bool fromNewLine = true;
            int i = 0;
            char[] delims = new char[] { '{', '}', '\n' };
            int next = value.IndexOfAny(delims);
            while (i <= next) {
                switch (value[next]) {
                    case '{':
                        Append(value.Substring(i, next - i + 1), fromNewLine);
                        fromNewLine = false;
                        AddTab();
                        break;
                    case '}':
                        RemoveTab();
                        Append(value.Substring(i, next - i + 1), fromNewLine);
                        fromNewLine = false;
                        break;
                    default://line break
                        Append(value.Substring(i, next - i + 1), fromNewLine);
                        fromNewLine = true;
                        break;
                }
                i = next + 1;
                next = value.IndexOfAny(delims, next + 1);
            }
            if (i < value.Length) {
                Append(value.Substring(i) + "\n", fromNewLine);
            }
        }
        public override string ToString() {
            return sb.ToString();
        }
    }
}
